/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem.internal;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.kommando.core.controlcenter.PreferencePage;
import org.kommando.core.osgi.BaseActivator;
import org.kommando.filesystem.FileSystemBrowser;
import org.kommando.filesystem.actions.GetFileLocationAction;
import org.kommando.filesystem.actions.GetFilePathAction;
import org.kommando.filesystem.actions.GetFileURLAction;
import org.kommando.filesystem.actions.NewFolderAction;
import org.kommando.filesystem.actions.OpenAction;
import org.kommando.filesystem.actions.RenameAction;
import org.kommando.filesystem.actions.RevealAction;
import org.kommando.filesystem.core.FileSource;
import org.kommando.filesystem.objectsource.FileSystemObjectSource;
import org.kommando.filesystem.prefs.FileSystemPreferencePage;
import org.kommando.util.osgi.ServiceUtils;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Activator extends BaseActivator {

    private ServiceTracker fileSystemBrowserTracker;

    public void start(BundleContext context) throws Exception {
        fileSystemBrowserTracker = new ServiceTracker(context, FileSystemBrowser.class.getName(), null);
        fileSystemBrowserTracker.open();

        FileSystemBrowser browser = ServiceUtils.getService(context, FileSystemBrowser.class);

        // register the actions
        registerAction(context, new RevealAction(browser));
        registerAction(context, new OpenAction());
        registerAction(context, new RenameAction());
        registerAction(context, new NewFolderAction());
        registerAction(context, new GetFileLocationAction());
        registerAction(context, new GetFilePathAction());
        registerAction(context, new GetFileURLAction());

        // register the objectsource
        FileSystemObjectSource fileSystemObjectSource = createObjectSource(context.getBundle().getResource("data/config.xml"));
        registerObjectSource(context, fileSystemObjectSource);

        context.registerService(PreferencePage.class.getName(), new FileSystemPreferencePage(fileSystemObjectSource), null);
    }

    private FileSystemObjectSource createObjectSource(URL config) throws IOException, SAXException, ParserConfigurationException {
        FileSystemObjectSource fileSystemObjectSource = new FileSystemObjectSource();

        InputStream in = config.openStream();
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
        in.close();

        Element rootElement = document.getDocumentElement();
        NodeList sourceElements = rootElement.getElementsByTagName("fileSource");
        for (int i = 0; i < sourceElements.getLength(); i++) {
            Element sourceElement = (Element) sourceElements.item(i);

            String source = sourceElement.getAttribute("source");
            boolean includeSource = Boolean.parseBoolean(sourceElement.getAttribute("includeSource"));
            boolean includeHiddenFiles = Boolean.parseBoolean(sourceElement.getAttribute("includeHiddenFiles"));
            int depth = Integer.parseInt(sourceElement.getAttribute("depth"));
            List<String> types = Arrays.asList(sourceElement.getAttribute("types").split(","));

            FileSource fileSource = new FileSource(source, includeSource, includeHiddenFiles, depth, types);
            fileSystemObjectSource.addFileSource(fileSource);
        }

        return fileSystemObjectSource;
    }

    public void stop(BundleContext context) throws Exception {
        fileSystemBrowserTracker.close();
    }

}
